home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter3 / sconopt.c < prev    next >
C/C++ Source or Header  |  1996-04-27  |  8KB  |  239 lines

  1.  
  2. #include <windows.h>  
  3. #include "sconopt.h"
  4. #include "sqlext.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "SQLSetConnectOption()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HENV hEnv  = NULL;
  110. static HDBC hDBC  = NULL;
  111. static HWND hList = NULL;
  112.  
  113.    switch( uMsg )
  114.    {
  115.       case WM_CREATE  :
  116.               // Allocate Environment and Connection.
  117.               //.....................................
  118.               SQLAllocEnv( &hEnv );
  119.               SQLAllocConnect( hEnv, &hDBC );
  120.  
  121.               hList = CreateWindow( "LISTBOX", "",    
  122.                                     LBS_NOTIFY | WS_VSCROLL | 
  123.                                     WS_BORDER  | WS_CHILD | 
  124.                                     WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  125.                                     0, 0, 
  126.                                     0, 0,  
  127.                                     hWnd,              
  128.                                     (HMENU)101,              
  129.                                     hInst,         
  130.                                     NULL               
  131.                                   );
  132.  
  133.               break;
  134.  
  135.       case WM_SIZE :
  136.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  137.                                        HIWORD( lParam ), TRUE );
  138.               break; 
  139.               
  140.       case WM_COMMAND :
  141.               switch( LOWORD( wParam ) )
  142.               {
  143.                  case IDM_TEST :
  144.                         {
  145.                            RETCODE retCode;
  146.  
  147.                            // Connect to the data source
  148.                            //...........................
  149.                            retCode = SQLConnect( hDBC, 
  150.                                                  "Test Data Source", SQL_NTS,
  151.                                                  NULL, 0,
  152.                                                  NULL, 0 );
  153.  
  154.                            // Show connection status.
  155.                            //........................
  156.                            if( retCode == SQL_SUCCESS || 
  157.                                retCode == SQL_SUCCESS_WITH_INFO )
  158.                            {
  159.                               SendMessage( hList, LB_ADDSTRING, 
  160.                                  0, (LPARAM)"Connection established." );
  161.  
  162.                               retCode = SQLSetConnectOption( hDBC, 
  163.                                  SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF );
  164.  
  165.                               if( retCode == SQL_SUCCESS || 
  166.                                   retCode == SQL_SUCCESS_WITH_INFO )
  167.                               {
  168.                                  SendMessage( hList, LB_ADDSTRING, 
  169.                                     0, (LPARAM)"AutoCommit mode is now off.");
  170.                               }
  171.                               else
  172.                               {
  173.                                  SendMessage( hList, LB_ADDSTRING, 
  174.                                     0, (LPARAM)"SQLSetConnectOption() failed.");
  175.                               }
  176.                            }
  177.                            else
  178.                            {
  179.                               SendMessage( hList, LB_ADDSTRING, 
  180.                                  0, (LPARAM)"Unable to establish connection" );
  181.                            }
  182.                         }
  183.                         break;
  184.  
  185.                  case IDM_ABOUT :
  186.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  187.                         break;
  188.  
  189.                  case IDM_EXIT :
  190.                         DestroyWindow( hWnd );
  191.                         break;
  192.               }
  193.               break;
  194.       
  195.       case WM_DESTROY :
  196.               // Disconnect Environment.
  197.               //........................
  198.               if ( hDBC )
  199.                  SQLDisconnect( hDBC );
  200.  
  201.               // Free Connection and Environment.
  202.               //.................................
  203.               SQLFreeConnect( hDBC );
  204.               SQLFreeEnv( hEnv );
  205.  
  206.               PostQuitMessage(0);
  207.               break;
  208.  
  209.       default :
  210.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  211.    }
  212.  
  213.    return( 0L );
  214. }
  215.  
  216.  
  217. LRESULT CALLBACK About( HWND hDlg,           
  218.                         UINT message,        
  219.                         WPARAM wParam,       
  220.                         LPARAM lParam)
  221. {
  222.    switch (message) 
  223.    {
  224.        case WM_INITDIALOG: 
  225.                return (TRUE);
  226.  
  227.        case WM_COMMAND:                              
  228.                if (   LOWORD(wParam) == IDOK         
  229.                    || LOWORD(wParam) == IDCANCEL)    
  230.                {
  231.                        EndDialog(hDlg, TRUE);        
  232.                        return (TRUE);
  233.                }
  234.                break;
  235.    }
  236.  
  237.    return (FALSE); 
  238. }
  239.